package chapter14.core;

import java.lang.reflect.Method;


/**
 * reflect utils.
 *
 * @author Lingo
 */
public class ReflectUtils {
    /** protected constructor. */
    protected ReflectUtils() {
    }

    /**
     * get method value by name.
     *
     * @param target Object
     * @param methodName method name
     * @return object
     * @throws Exception ex
     */
    public static Object getMethodValue(Object target, String methodName)
        throws Exception {
        Method method = target.getClass().getDeclaredMethod(methodName);

        return method.invoke(target);
    }

    /**
     * set method value by name.
     *
     * @param target Object
     * @param methodName method name
     * @param methodValue method value
     * @throws Exception ex
     */
    public static void setMethodValue(Object target, String methodName,
        Object methodValue) throws Exception {
        Method method = target.getClass()
                              .getDeclaredMethod(methodName,
                methodValue.getClass());
        method.invoke(target, methodValue);
    }
}
